/**
 * @Title: login
 * @Package mall-fe
 * @author Shinelon
 * @date 2017/9/6 13:23
 * @version V1.0
 */
"use strict";
require('./index.css');
require('js/common/nav/index.js');
require('js/common/header/index.js');
var navSide = require('js/common/nav-side/index');
var shiqla =  require('util/shiqlamall.js');
var userService = require('service/user-service.js');

// page 逻辑部分
var page = {
    init: function () {
        this.onLoad();
        this.bindEvent();
    },
    onLoad: function () {
       userService.checkLogin(function () {
           //校验用户是否已经登录,如果登录则初始化修改密码界面
           // 初始化左侧菜单
           navSide.init({
               name: 'user-pass-update'
           });
       },function () {
           //如果用户没有登录,跳转到主页
           window.location.href = './result.html?type=updatepass';
       });
    },
    bindEvent: function () {
        var _this = this;
        // 点击提交按钮后的动作
        $(document).on('click', '.btn-submit', function () {
            var userInfo = {
                    password: $.trim($('#password').val()),
                    passwordNew: $.trim($('#password-new').val()),
                    passwordConfirm: $.trim($('#password-confirm').val())
                },
                validateResult = _this.validateForm(userInfo);
            if (validateResult.status) {
                // 更改用户密码
                userService.updatePassword({
                    passwordOld: userInfo.password,
                    passwordNew: userInfo.passwordNew
                }, function (res, msg) {
                    shiqla.successTips(msg);
                }, function (errMsg) {
                    shiqla.errorTips(errMsg);
                });
            }
            else {
                shiqla.errorTips(validateResult.msg);
            }
        });
    },
    // 验证字段信息
    validateForm: function (formData) {
        var result = {
            status: false,
            msg: ''
        };
        // 验证原密码是否为空
        if (!shiqla.validate(formData.password, 'require')) {
            result.msg = '原密码不能为空';
            return result;
        }
        // 验证新密码长度
        if (!formData.passwordNew || formData.passwordNew.length < 6) {
            result.msg = '密码长度不得少于6位';
            return result;
        }
        // 验证两次输入的密码是否一致
        if (formData.passwordNew !== formData.passwordConfirm) {
            result.msg = '两次输入的密码不一致';
            return result;
        }
        // 通过验证，返回正确提示
        result.status = true;
        result.msg = '验证通过';
        return result;
    }
};
$(function () {
    page.init();
});